/*
 * @lc app=leetcode.cn id=38 lang=cpp
 *
 * [38] 外观数列
 */

// @lc code=start
#include <string>
using namespace std;
class Solution
{
public:
    string countAndSay(int n)
    {
        if (n <= 0)
            return "";
        string res = "1";
        while (--n)
        {
            string cur = "";
            for (int i = 0; i < res.size(); i++)
            {
                int cnt = 1;
                while (i + 1 < res.size() && res[i] == res[i + 1])
                {
                    cnt++;
                    i++;
                }
                cur += to_string(cnt) + res[i];
            }
            res = cur;
        }
        return res;
    }
};
// @lc code=end
